package com.finance.system.file.entity;

import com.finance.system.file.constant.FileConstant;
import com.finance.system.support.utils.ConfigUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

@Data
@Accessors(chain = true)
@AllArgsConstructor
public class PhysicalFile{
    private FileSystemEntity fileSystem;

    private InputStream fileStream;

    public static PhysicalFile from(FileSystemEntity fileSystemEntity){
        return new PhysicalFile(fileSystemEntity,null);
    }

    public Path toPath(){
        FileSystemEntity fileSystem = this.getFileSystem();
        Path path = Paths.get(ConfigUtils.getConfig(FileConstant.FILE_BASE_DIR))
                .resolve(fileSystem.getParentFolder())
                .resolve(fileSystem.getYearFolder().toString())
                .resolve(fileSystem.getMonthFolder().toString())
                .resolve(fileSystem.getId().toString() + "."+ fileSystem.getFileExtension());
        return path;


    }
}
